package net.xiduth.game.player.content.misc;

import java.util.HashMap;
import java.util.Map;

import net.xiduth.game.World;
import net.xiduth.game.item.Item;
import net.xiduth.game.player.Player;
import net.xiduth.utils.Misc;

/**
 * @author Tyluur <itstyluur@gmail.com>
 * @since 2012-08-23
 */
public class PvPArtifacts {

	/**
	 * An enum containing all of the data regarding ancient artefacts from
	 * http://runescape.wikia.com/wiki/Ancient_artefacts
	 * 
	 * @author Tyluur<itstyluur@gmail.com>
	 * @since 2012-08-23
	 */
	public enum Artefact {

		BROKEN_STATUE_HEADDRESS(14892, 5000),

		THIRD_AGE_CARAFE(14891, 10000),

		BRONZED_DRAGON_CLAW(14890, 20000),

		ANCIENT_PSALTERY_BRIDGE(14889, 30000),

		SARADOMIN_AMPHORA(14888, 40000),

		BANDOS_SCHRIMSHAW(14887, 50000),

		SARADOMIN_CARVING(14886, 75000),

		ZAMORAK_MEDALLION(14885, 10000),

		ARMADYL_TOTEM(14884, 150000),

		GUTHIXIAN_BRAZIER(14883, 200000),

		RUBY_CHALICE(14882, 250000),

		BANDOS_STATUETTE(14881, 300000),

		SARADOMIN_STATUETTE(14880, 400000),

		ZAMORAK_STATUETTE(14879, 500000),

		ARMADYL_STATUETTE(14878, 750000),

		SEREN_STATUETTE(14877, 1000000),

		ANCIENT_STATUETTE(14876, 5000000);

		private int id, reward;

		Artefact(int id, int reward) {
			this.id = id;
			this.reward = reward;
		}

		private static final Map<Integer, Artefact> MAP = new HashMap<Integer, Artefact>();

		static {
			for (Artefact artefact : Artefact.values()) {
				MAP.put(artefact.getId(), artefact);
			}
		}

		public static final Artefact forId(int id) {
			return MAP.get(id);
		}

		public int getReward() {
			return reward;
		}

		public void setReward(int reward) {
			this.reward = reward;
		}

		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}
	}

	/**
	 * The method to return the amount of coins you should be rewarded for all of your ancient artefacts in your inventory.
	 * @param player
	 * @return The amount of given coins.
	 */
	public static int getArtifactReward(Player player) {
		int reward = 0;
		for (Item item : player.getInventory().getItems().toArray()) {
			if (item == null)
				continue;
			Artefact artefact = Artefact.forId(item.getId());
			if (artefact != null) {
				reward += artefact.getReward();
			}
		}
		return reward;
	}

	public static void convertToCoins(Player player) {
		for (Item item : player.getInventory().getItems().toArray()) {
			if (item == null)
				continue;
			Artefact artefact = Artefact.forId(item.getId());
			if (artefact != null) {
				if (item.getId() == artefact.getId()) {
					player.getMoneyPouch().add(getArtifactReward(player));
					player.getInventory().deleteItem(item);
				}
			}
		}
	}

	public static boolean hasArtefacts(Player player) {
		boolean hasArtefacts = false;
		for (Item item : player.getInventory().getItems().toArray()) {
			if (item == null)
				continue;
			Artefact artefact = Artefact.forId(item.getId());
			if (artefact != null)
				hasArtefacts = true;
		}
		return hasArtefacts;
	}

	public static final Item artefactToDrop(Player player) {
		if (player == null || player.getPlayerData() == null || player.getPlayerData().getEarningPotential() == -1)
			return null;
		Item item = new Item(player.getPlayerData().getEarningPotential() > 90 ? 14876 + Misc.random(16) : 14892 - Misc.random(5));
		return item;
	}

	public static void sendDeath(Player player, Player killer) {
		if (artefactToDrop(killer) != null) {
			World.addGroundItem(artefactToDrop(killer), player.getWorldTile(), killer, false, 180, true);
			if (Artefact.forId(artefactToDrop(killer).getId()) != null) {
				int epl = Artefact.forId(artefactToDrop(killer).getId()).ordinal() * 10;
				killer.getPlayerData().setEarningPotential((player.getPlayerData().getEarningPotential() - epl >= 0 ? player.getPlayerData().getEarningPotential() - epl : 0));
				killer.sendMessage("<col=FF0000>You have just lost " + epl + " earning potential.");
			}
		}
	}

}
